package com.dzxcqmxm.servlet.photo;

import com.dzxcqmxm.Dao.AlbumDao;
import com.dzxcqmxm.Dao.PhotoDao;
import com.dzxcqmxm.Dao.impl.AlbumDaoImpl;
import com.dzxcqmxm.Dao.impl.PhotoDaoImpl;
import com.dzxcqmxm.JB.Album;
import com.dzxcqmxm.JB.Photo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 展示所有的相片的servlet
 * 得注入一下，
 * web.xml
 */
public class findPhotoByUserNameAndAlbumUrlServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 获取链接的参数。从yhmenu直接就到这里了
         */
        System.out.println("打印连接上获取的AlbumUrl"+request.getParameter("AlbumUrl"));
        String a = request.getParameter("AlbumUrl");
        System.out.println("a，也就是再fandPhoto页面的"+a);
        String XTXCName = request.getParameter("XTXCName");
        System.out.println("XTXCName，也就是再fandPhoto页面的"+XTXCName);
        String AlbumName = request.getParameter("AlbumName");
        System.out.println("AlbumName，也就是再fandPhoto页面的"+AlbumName);


        PhotoDao photoDao = new PhotoDaoImpl();
        Photo photo = new Photo();
        ArrayList<Photo> photoArrayList = new ArrayList<Photo>();


        //获取页面跳转的时候携带的参数
        /**
         * 因为这个页面主要是给展示页面，session直接来的是这里，所以可以直接获取
         * session
         */
        String AlbumUrl = a;
        /**
         * 执行方法前判断用户，先获取全局变量username
         */
        ServletContext context = this.getServletContext();
        //获取数据，类似键值对，需要通过键名获取
        String username=(String) context.getAttribute("username");
        System.out.println("打印全局变量username" +username);

        try {
            photoArrayList = photoDao.findPhotoByUserNameAndAlbumUrl(username,AlbumUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }


        System.out.println("打印photoArrayList"+photoArrayList);


        //session会话对象可以数据共享于一次会话这个给到我们的，展示照片页面来获取
        request.getSession().setAttribute("photoArrayList", photoArrayList);
        //这个给那个，照片展示页面设置的
        request.getSession().setAttribute("AlbumUrl", AlbumUrl);
        request.getSession().setAttribute("XTXCName", XTXCName);
        request.getSession().setAttribute("AlbumName", AlbumName);

        response.sendRedirect("/dzxc/YongHu/yhPhoto/yhDisplayPhoto.jsp");

    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            doPost(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
}
